package com.zhuozhou.jdyc.main.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.AppSettingHelper;
import com.zhuozhou.jdyc.api.ConstantsHelper;
import com.zhuozhou.jdyc.base.BaseFragment;
import com.zhuozhou.jdyc.databinding.SettingBinding;
import com.zhuozhou.jdyc.main.base.activity.FingerViewActivity;
import com.zhuozhou.jdyc.main.base.activity.LockViewActivity;
import com.zhuozhou.jdyc.main.user.viewmodel.SettingViewModel;
import com.zhuozhou.jdyc.util.apiUtil.GetVersion;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.wight.dialog.ConfirmDialog;

import static android.app.Activity.RESULT_OK;
import static com.zhuozhou.jdyc.api.ConstantsHelper.FINGER_VIEW_ACTIVITY_REQUEST_CODE;
import static com.zhuozhou.jdyc.api.ConstantsHelper.LOOK_VIEW_ACTIVITY_REQUEST_CODE;

/**
 * Created by 方舟 on 2017/5/24.
 * 设置页面
 */
public class SettingFragment extends BaseFragment<SettingViewModel, SettingBinding> {

    @Override
    protected int getLayoutId() {
        return R.layout.setting;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        binding.versionName.setText("v" + GetVersion.getVersion(getActivity()));
        binding.setSetViewModel(mViewModel);
        binding.gesturesPassword.setChecked(AppSettingHelper.getGesturesIsOpen());
        binding.gesturesPassword.setOnClickListener(v -> {
            if (binding.gesturesPassword.isChecked()) showGesturesDialog("是否开启手势密码");
            else showGesturesDialog("是否关闭手势密码");
        });
        LogUtil.show("FingerIsOpen: " + AppSettingHelper.getFingerIsOpen());
        binding.fingerprintPassword.setChecked(AppSettingHelper.getFingerIsOpen());
        binding.fingerprintPassword.setOnClickListener(v -> {
            if (mViewModel.supportFingerprint()) {
                if (binding.fingerprintPassword.isChecked()) showFingerDialog("是否开启指纹密码");
                else showFingerDialog("是否关闭指纹密码");
            } else {
                binding.fingerprintPassword.setChecked(AppSettingHelper.getFingerIsOpen());
            }
        });
    }

    @Override
    protected void initData(Bundle bundle) {

    }

    private void showFingerDialog(String message) {
        new ConfirmDialog(getActivity())
                .setMessage(message)
                .setShowCancelView(false)
                .setOnSureClickListener(v -> {
                    LogUtil.show("FingerIsOpen: " + AppSettingHelper.getFingerIsOpen());
                    if (AppSettingHelper.getFingerIsOpen()) {
                        AppSettingHelper.setFingerIsOpen(false);
                    } else {
                        Intent intent = new Intent(getActivity(), FingerViewActivity.class);
                        startActivityForResult(intent, ConstantsHelper.FINGER_VIEW_ACTIVITY_REQUEST_CODE);
                    }
                    binding.fingerprintPassword.setChecked(AppSettingHelper.getFingerIsOpen());
                }).builder().show();
    }


    private void showGesturesDialog(String message) {
        new ConfirmDialog(getActivity())
                .setMessage(message)
                .setShowCancelView(false)
                .setOnSureClickListener(v -> {
                    if (AppSettingHelper.getGesturesIsOpen()) {
                        AppSettingHelper.setGesturesIsOpen(false);
                        AppSettingHelper.setGestures("");
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("isSet", true);
                        Intent intent = new Intent(getActivity(), LockViewActivity.class);
                        intent.putExtras(bundle);
                        startActivityForResult(intent, ConstantsHelper.LOOK_VIEW_ACTIVITY_REQUEST_CODE);
                    }
                }).builder().show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        LogUtil.show("requestCode: " + requestCode + "resultCode: " + resultCode);
        if (requestCode == LOOK_VIEW_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                binding.gesturesPassword.setChecked(AppSettingHelper.getGesturesIsOpen());
            }
        } else if (requestCode == FINGER_VIEW_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                binding.fingerprintPassword.setChecked(AppSettingHelper.getFingerIsOpen());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public static Fragment instantiate(Bundle bundle) {
        Fragment fragment = new SettingFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
